import java.util.Arrays;

public class arrayTest {
    //字符串拷贝
    //模拟类方法
    public static int[] my_copyOf(int[] array,int lenth) {
        int[] tmp = new int[array.length];
        System.arraycopy(array,0,tmp,0,lenth);
        return tmp;

    }

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};

        int[] array2 = array;
        //这个是不是拷贝？ 不算拷贝 根本没有 产生新的内存空间

        //原始方法实现
        int[] array3 = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            array3[i] = array[i];
        }

        //使用类方法
        int[] array4 = Arrays.copyOf(array,array.length);
        //扩容两倍（后面没赋值的都是默认值）
        int[] array5 = Arrays.copyOf(array,2*array.length);



        int[] array6 = new int[array.length];
        System.arraycopy(array,1,array6,0,array.length-1);
        System.arraycopy(array,1,array6,1,array.length-1);
        System.arraycopy(array,1,array6,3,array.length-3);


//        System.out.println(Arrays.toString(array));
//        System.out.println(Arrays.toString(array3));
//        System.out.println(Arrays.toString(array4));
        System.out.println(Arrays.toString(array6));
    }



    //数组转字符串
    //1.使用类方法
    public static void main2(String[] args) {
        int[] array = null;
        String arr = Arrays.toString(array);
        System.out.println(arr);
    }
    //2.模拟实现
    public static String my_ToString(int[] array) {
        //当array为空时返回null
        if(array == null)
            return "null";
        int iMax = array.length-1;
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str += array[i];
            if(i != array.length-1){
                str += ",";
            }
        }
        str += "]";
        return str;
    }

    public static void main1(String[] args) {
        int[] array = new int[0];
        System.out.println(my_ToString(array));
    }

}
